package com.dayuanit.dy16.ebook.ebookweb.task;

import com.dayuanit.dy16.ebook.ebookweb.entity.Order;
import com.dayuanit.dy16.ebook.ebookweb.enums.OrderStatusEnum;
import com.dayuanit.dy16.ebook.ebookweb.mapper.OrderMapper;
import com.dayuanit.dy16.ebook.ebookweb.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 取消订单 任务
 * 创建时间到当前时间 1分钟后 过期
 */
@Component
public class CancelTask extends BaseTask {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IOrderService orderService;

    @Override
    @Scheduled(cron = "0/60 * * * * ?")
    protected void doTask() {
//        process();
    }

//    private void process() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.SECOND, -120);
//        Date deadTime = calendar.getTime();
//
//        System.out.println(">>>>>>>>>CancelTask");
//        List<Order> orders = orderMapper.listByStatus(OrderStatusEnum.wait_pay.getK(), null);
//        for (Order order : orders) {
//            if (deadTime.before(order.getCreateTime())) {
//                continue;
//            }
//
//            logger.info("订单{}过期", order.getId());
//
//            try {
//                orderService.processCancelOrder(order);
//            } catch(Exception e) {
//                logger.error(e.getMessage(), e);
//            }
//        }
//    }
}
